﻿using System;
using System.ComponentModel;


namespace OverlakeApp.ViewModels
{
    /// <summary>
    /// A base ViewModel that implements basic ViewModel functionality.
    /// </summary>
    public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
    {
        public static System.Windows.Threading.Dispatcher Dispatcher { get; set; }

        /// <summary>
        /// If the app has been loaded by the designer, theis property is <value>true</value>.
        /// </summary>
        public static bool IsInDesignMode
        {
            get
            {
#if PC
                return DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject());
#else
#if Silverlight
                return !HtmlPage.IsEnabled;
#else
                return DesignerProperties.IsInDesignTool;
#endif
#endif
            }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;


        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or
        /// resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {

        }

        /// <summary>
        /// Used to properly invoke the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);

                if (Dispatcher != null)                             // If the dispatcher is set, use that
                    Dispatcher.BeginInvoke(new Action(() => handler(this, e)));
                else                                                    // Otherwise give up and execute on the calling thread.
                    handler(this, e);
            }
        }
    }
}